﻿using System;
using System.Collections.Generic;
using System.Collections;
using UnityEngine;

namespace Battle.View
{
    public class CGuideBattleViewCtrl : BattleViewCtrl
    {

        public enum GuideStep
        {
            None,                   // None
            Kirito_FirstSkill_Cast,             // 提示桐人释放第一个技能
            Kirito_FirstSkill_Casting_Pre,      // 桐人第一个技能释放中（前半段）
            Kirito_FirstSkill_Casting_Pause,    // 桐人第一个技能释放中的暂停 （击飞）
            MainHero_Combo_FirstSkill_Cast,     // 提示主角Combo击飞
            First_Combo_Casting_Pre,            // Combo进行中（前半段）
            First_Combo_Casting_Pause,          // 讲解掉落魔晶石
            First_Combo_Casting_Last,           // Combo继续
            Kirito_SecondSkill_Cast,            // 提示桐人释放第二个技能
            Kirito_SecondSkill_Casting_Pre,     // 桐人第二个技能释放中（前半段）
            Kirito_SecondSkill_Casting_Pause,   // 桐人第二个技能释放中的暂停（倒地） 
            Asuna_Combo_SecondSkill_Cast,       // 提示主角Comobo倒地
            Second_Combo_Casting_Pre,           // Combo进行中（前半段）
            Second_Combo_Casting_Pause,         // 讲解掉落炎魔石
            Second_Combo_Casting_Last,          // Combo继续
            GuideOver,                          // 教程结束
        }

        public const string kiritoHeroId = "hero_001";
        public const string asunaHeroId = "hero_007";

        public GuideStep curGuideStep = GuideStep.None;

        public bool isKiritoFirstSkillGuideShow = false;
        public bool isKiritoSecondSKillGuideShow = false;
        public bool isGudieOverShow = false;

        public override void Init(BattleManager m)
        {
            base.Init(m);
            curGuideStep = GuideStep.None;
            isKiritoFirstSkillGuideShow = false;
            isKiritoSecondSKillGuideShow = false;
            isGudieOverShow = false;
            isAngerBallDropped = false;
            isFireBallDropped = false;
            isAngerBallGuideShow = false;
            isFireBallGuideShow = false;
            SetIsHaveAutoFightTimeout(false);
        }

        public override void InitState()
        {
            allStates = new Dictionary<BattleState, Fsm.FsmStateMonoBase<BattleState>>();
            allStates[BattleState.ResourceLoadState] = new CGuideResourceLoadState(BattleState.ResourceLoadState, this);
            allStates[BattleState.EntranceState] = new EntranceState(BattleState.EntranceState, this);
            allStates[BattleState.BattleRunningState] = new CGuideBattleRunningState(BattleState.BattleRunningState, this);
            allStates[BattleState.ResultState] = new ResultState(BattleState.ResultState, this);
        }

        public void OnGuidePanelClick()
        {
            if (curGuideStep == GuideStep.Kirito_FirstSkill_Casting_Pause)
            {
                curGuideStep = GuideStep.MainHero_Combo_FirstSkill_Cast;
                BattleCharacter bc = null;
                foreach (var actor in GetAllActors())
                {
                    if (actor.Value.heroClass == (int)HeroClass.Main_Actor_Man ||
                        actor.Value.heroClass == (int)HeroClass.Main_Actor_Woman)
                    {
                        bc = actor.Value;
                        break;
                    }
                }
                if (bc != null)
                {
                    var heroItem = BattleManager.Instance.battlePanel.GetBattleHeroItem(bc.uniqueId);
                    BattleManager.Instance.battlePanel.guidePanel.Show(heroItem, true, GuideStep.MainHero_Combo_FirstSkill_Cast);
                }
                else
                {
                    GuideClose();
                }
            }
            else if (curGuideStep == GuideStep.First_Combo_Casting_Pause)
            {
                Time.timeScale = 1f;
                curGuideStep = GuideStep.First_Combo_Casting_Last;
                BattleManager.Instance.battlePanel.guidePanel.Show(null, false, GuideStep.First_Combo_Casting_Last);
                BallRevertFromUI();
            }
            else if (curGuideStep == GuideStep.Kirito_SecondSkill_Casting_Pause)
            {
                curGuideStep = GuideStep.Asuna_Combo_SecondSkill_Cast;
                BattleCharacter bc = null;
                foreach (var actor in GetAllActors())
                {
                    if (actor.Value.heroId == asunaHeroId)
                    {
                        bc = actor.Value;
                        break;
                    }
                }
                if (bc != null)
                {
                    var heroItem = BattleManager.Instance.battlePanel.GetBattleHeroItem(bc.uniqueId);
                    BattleManager.Instance.battlePanel.guidePanel.Show(heroItem, true, GuideStep.Asuna_Combo_SecondSkill_Cast);
                }
                else
                {
                    GuideClose();
                }
            }
            else if (curGuideStep == GuideStep.Second_Combo_Casting_Pause)
            {
                Time.timeScale = 1f;
                curGuideStep = GuideStep.Second_Combo_Casting_Last;
                BattleManager.Instance.battlePanel.guidePanel.Show(null, false, GuideStep.Second_Combo_Casting_Last);
                BallRevertFromUI();

            }
            else if (curGuideStep == GuideStep.GuideOver)
            {
                GuideClose();
            }
        }

        public void GuideClose()
        {
            curGuideStep = GuideStep.None;
            BattleManager.Instance.battlePanel.guidePanel.Hide();
        }

        public bool isAngerBallDropped = false;
        public bool isFireBallDropped = false;

        bool isAngerBallGuideShow = false;
        bool isFireBallGuideShow = false;

        public void AddDropWaterBall(BattleCharacter bc, BattleCharacter targetBC, float hpercent, Vector3 dir, Vector3 speed)
        {
            var ball = BattleFactory.SpawnDropBall();
            ball.GetComponent<DropAngerBall>().Drop(bc, targetBC, hpercent, dir, speed);
            OnDropAngerBall(ball);
        }

        public void AddDropFireBall(BattleCharacter bc, BattleCharacter targetBC, float hpercent, Vector3 dir, Vector3 speed)
        {
            var ball = BattleFactory.SpawnDropFireBall();
            ball.GetComponent<DropFireBall>().Drop(bc, targetBC, hpercent, dir, speed);
            OnDropFireBall(ball);
        }

        public override void OnDropAngerBall(GameObject ball)
        {
            if (curGuideStep == GuideStep.First_Combo_Casting_Pre && !isAngerBallGuideShow)
            {
                isAngerBallGuideShow = true;
                StartCoroutine(DropAngerBallDelay(ball));
            }
        }

        IEnumerator DropAngerBallDelay(GameObject ball)
        {
            yield return new WaitForSeconds(0.5f);
            Time.timeScale = 0f;
            curGuideStep = GuideStep.First_Combo_Casting_Pause;
            BattleManager.Instance.battlePanel.guidePanel.Show(null, false, GuideStep.First_Combo_Casting_Pause);
            BallShowInUI(ball);
        }


        public override void OnDropFireBall(GameObject ball)
        {
            if (curGuideStep == GuideStep.Second_Combo_Casting_Pre && !isFireBallGuideShow)
            {
                isFireBallGuideShow = true;
                StartCoroutine(DropFireBallDelay(ball));
            }
        }

        IEnumerator DropFireBallDelay(GameObject ball)
        {
            yield return new WaitForSeconds(0.5f);
            Time.timeScale = 0f;
            curGuideStep = GuideStep.Second_Combo_Casting_Pause;
            BattleManager.Instance.battlePanel.guidePanel.Show(null, false, GuideStep.Second_Combo_Casting_Pause);
            BallShowInUI(ball);
        }

        GameObject curUIBall = null;
        Vector3 pausePos;

        void BallShowInUI(GameObject ball)
        {
            curUIBall = ball;
            BattleCamera.Instance.SetFocusGameObject(curUIBall);
        }

        void BallRevertFromUI()
        {
            if (curUIBall != null)
            {
                BattleCamera.Instance.SetUnfocusGameObject(curUIBall, LayerName.SceneParticle);
            }
            curUIBall = null;
        }

    }
}
